package com.aliyun.heiyu.powermaster.server.rotary.dao.impl;

import com.aliyun.heiyu.powermaster.server.rotary.contants.WarnSqlContant;
import com.aliyun.heiyu.powermaster.server.rotary.dao.WarnDAO;
import com.aliyun.heiyu.powermaster.server.rotary.object.entity.CoilEntity;
import com.aliyun.heiyu.powermaster.server.rotary.service.impl.WarnServiceImpl;
import com.aliyun.heiyu.powermaster.server.rotary.utils.JdbcUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 告警接口实现类
 * @Author shun.zhang
 * @Date 2020/1/8 16:55
 **/
public class WarnDAOImpl implements WarnDAO {
    @Override
    public List<CoilEntity> queryCoils() {
        List<CoilEntity> coilEntities = JdbcUtil.queryForList(WarnSqlContant.QUERY_COILS, null, CoilEntity.class);
        return coilEntities;
    }

    @Override
    public Integer saveWarn(CoilEntity coilEntity, String device_id) {
        Object[] obj = new Object[6];
        obj[0] = device_id;
        obj[1] = coilEntity.getAddress_value();
        obj[2] = coilEntity.getDescription();
        obj[3] = coilEntity.getCoil_type();
        obj[4] = 1;
        obj[5] = WarnServiceImpl.airDate.get(device_id);
        return JdbcUtil.insert(WarnSqlContant.SAVE_WARN, obj);
    }

    public Map<String, CoilEntity> getCoilsMap() {
        List<CoilEntity> coilEntities = queryCoils();
        if (null == coilEntities) {
            return null;
        }
        Map<String, CoilEntity> map = new HashMap<>(16);
        coilEntities.forEach(coilEntity -> {
            StringBuilder sbu = new StringBuilder();
            sbu.append(coilEntity.getCoil_type())
                .append("_")
                .append(coilEntity.getAddress());
            map.put(sbu.toString(), coilEntity);
        });
        return map;
    }
}
